package controllers.project;

import hprose.HproseClinetService;

import java.util.HashMap;
import java.util.Map;

import play.cache.Cache;
import utils.convert.Convert;
import utils.logs.LogsUtils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jamonapi.utils.Logger;

import component.UserBis;
import constants.APIConstants;
import constants.Constants;
import controllers.common.filter.BaseFilter;

public class ProjectManageAction  extends BaseFilter{
	
	public static void index(){
		JSONObject machines = new JSONObject();
		JSONObject cutters = new JSONObject();
		Map<String, Object> operatorMap = new HashMap<String, Object>();
		Object[] operatorArguments = new Object[] { operatorMap };
		operatorMap.put("customer_id", UserBis.currUser());
		operatorMap.put("roles", "RU");
		operatorMap.put("isPage", Constants.DATAISPAGE_NO);
		String operatorRes = HproseClinetService.synCall(APIConstants.OMSI_GETOPERATOR, operatorArguments,APIConstants.CUSTOMER_HOST, APIConstants.CUSTOMER_PORT);
		JSONObject operators = JSONObject.parseObject(operatorRes);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("customer_id", UserBis.currUser());
		map.put("isPage", "2");
		Object [] arguments = new Object[]{map};
		map.put("status", "1");
		Object [] arguments2 = new Object[]{map};
		String res = HproseClinetService.synCall(APIConstants.CSMI_GETCUTTERSPECS, arguments2, APIConstants.CUSTOMER_HOST, APIConstants.CUSTOMER_PORT);
		String res2 = HproseClinetService.synCall(APIConstants.MGMI_GETGROUPLIST, arguments, APIConstants.CUSTOMER_HOST, APIConstants.CUSTOMER_PORT);
		machines = JSONObject.parseObject(res2);
		JSONArray machineArr =machines.getJSONArray("data");
		for (int i = 0; i < machineArr.size(); i++) {
			JSONObject machine = machineArr.getJSONObject(i);
			machine.put("id", machine.get("gourp_id"));
			machine.put("value", machine.get("gourp_name"));
		}
		cutters = JSONObject.parseObject(res);
		JSONArray cutterArr =cutters.getJSONArray("data");
 		for (int i = 0; i < cutterArr.size(); i++) {
			JSONObject cutter = cutterArr.getJSONObject(i);
			cutter.put("id", cutter.get("material_id"));
			cutter.put("value",cutter.getString("long_specs_cust"));
			
		}
		JSONArray operatorArr =operators.getJSONArray("data");
		for (int i = 0; i < operatorArr.size(); i++) {
			JSONObject operator = operatorArr.getJSONObject(i);
			operator.put("id", operator.get("operator_id"));
			operator.put("value", operator.get("real_name"));
		}
		render(cutters,machines,operators);
	}
	/**
	 * 获取当前项目
	 * @Title:        getRoughProjectList 
	 * @Description:  TODO(这里用一句话描述这个方法的作用) 
	 * @param:            
	 * @return:       void    
	 * @throws 
	 * @author        caofan
	 * @Date          2016年1月3日 下午1:40:19
	 */
	public static void getRoughProjectList(){
		JSONObject plans = new JSONObject();
		String state = "'1','2'";
		Map<String, Object> planParams = new HashMap<String, Object>();
		planParams.put("customer_id", UserBis.currUser());
		planParams.put("project_states", state);
		Object [] arguments = new Object[]{planParams};
		String res = HproseClinetService.synCall(APIConstants.PJMSI_GETPROJECTNEWLIST, arguments, APIConstants.CUSTOMER_HOST, APIConstants.CUSTOMER_PORT);
		plans = JSONObject.parseObject(res);
		renderJSON(plans);
	}
	
	/**
	 * 获取项目详情
	 * 
	 * 2016年5月27日 下午4:32:22
	 * @auth liqi
	 */
	public static void getProjectById(){
		JSONObject rs = new JSONObject();
		Map<String, Object> planParams = new HashMap<String, Object>();
		planParams.put("customer_id", UserBis.currUser());
		planParams.put("project_id", params.get("project_id"));
		Object [] arguments = new Object[]{planParams};
		String res = HproseClinetService.synCall(APIConstants.PJMSI_GETPROJECTBYID, arguments, APIConstants.CUSTOMER_HOST, APIConstants.CUSTOMER_PORT);
		rs = JSONObject.parseObject(res);
		renderJSON(rs);
	}
	
	public static void succProjectIndex(){
		JSONObject machines = new JSONObject();
		JSONObject cutters = new JSONObject();
		Map<String, Object> operatorMap = new HashMap<String, Object>();
		Object[] operatorArguments = new Object[] { operatorMap };
		operatorMap.put("customer_id", UserBis.currUser());
		operatorMap.put("roles", "RU");
		operatorMap.put("isPage", Constants.DATAISPAGE_NO);
		String operatorRes = HproseClinetService.synCall(APIConstants.OMSI_GETOPERATOR, operatorArguments,APIConstants.CUSTOMER_HOST, APIConstants.CUSTOMER_PORT);
		JSONObject operators = JSONObject.parseObject(operatorRes);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("customer_id", UserBis.currUser());
		map.put("isPage", "2");
		Object [] arguments = new Object[]{map};
		map.put("status", "1");
		Object [] arguments2 = new Object[]{map};
		String res = HproseClinetService.synCall(APIConstants.CSMI_GETCUTTERSPECS, arguments2, APIConstants.CUSTOMER_HOST, APIConstants.CUSTOMER_PORT);
		String res2 = HproseClinetService.synCall(APIConstants.MGMI_GETGROUPLIST, arguments, APIConstants.CUSTOMER_HOST, APIConstants.CUSTOMER_PORT);
		machines = JSONObject.parseObject(res2);
		JSONArray machineArr =machines.getJSONArray("data");
		for (int i = 0; i < machineArr.size(); i++) {
			JSONObject machine = machineArr.getJSONObject(i);
			machine.put("id", machine.get("gourp_id"));
			machine.put("value", machine.get("gourp_name"));
		}
		cutters = JSONObject.parseObject(res);
		JSONArray cutterArr =cutters.getJSONArray("data");
		for (int i = 0; i < cutterArr.size(); i++) {
			JSONObject cutter = cutterArr.getJSONObject(i);
			cutter.put("id", cutter.get("material_id"));
			cutter.put("value", cutter.get("long_specs_cust"));
		}
		JSONArray operatorArr =operators.getJSONArray("data");
		for (int i = 0; i < operatorArr.size(); i++) {
			JSONObject operator = operatorArr.getJSONObject(i);
			operator.put("id", operator.get("operator_id"));
			operator.put("value", operator.get("real_name"));
		}
		render(cutters,machines,operators);
	}
	/**
	 * 获取已完成项目
	 * @Title:        getSuccProjectList 
	 * @Description:  TODO(这里用一句话描述这个方法的作用) 
	 * @param:            
	 * @return:       void    
	 * @throws 
	 * @author        caofan
	 * @Date          2016年1月3日 下午1:40:11
	 */
	public static void getSuccProjectList(){
		JSONObject plans = new JSONObject();
		String state = "4";
		Map<String, Object> planParams = new HashMap<String, Object>();
		planParams.put("customer_id", UserBis.currUser());
		planParams.put("project_states", state);
		planParams.put("project_name",params.get("project_name"));
		Object [] arguments = new Object[]{planParams};
		String res = HproseClinetService.synCall(APIConstants.PJMSI_GETPROJECTNEWLIST, arguments, APIConstants.CUSTOMER_HOST, APIConstants.CUSTOMER_PORT);
		plans = JSONObject.parseObject(res);
		renderJSON(plans); 
	}
	
	public static void noStartProjectIndex(){
		JSONObject machines = new JSONObject();
		JSONObject cutters = new JSONObject();
		Map<String, Object> operatorMap = new HashMap<String, Object>();
		Object[] operatorArguments = new Object[] { operatorMap };
		operatorMap.put("customer_id", UserBis.currUser());
		operatorMap.put("roles", "RU");
		operatorMap.put("isPage", Constants.DATAISPAGE_NO);
		String operatorRes = HproseClinetService.synCall(APIConstants.OMSI_GETOPERATOR, operatorArguments,APIConstants.CUSTOMER_HOST, APIConstants.CUSTOMER_PORT);
		JSONObject operators = JSONObject.parseObject(operatorRes);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("customer_id", UserBis.currUser());
		map.put("isPage", "2");
		Object [] arguments = new Object[]{map};
		map.put("status", "1");
		Object [] arguments2 = new Object[]{map};
		String res = HproseClinetService.synCall(APIConstants.CSMI_GETCUTTERSPECS, arguments2, APIConstants.CUSTOMER_HOST, APIConstants.CUSTOMER_PORT);
		String res2 = HproseClinetService.synCall(APIConstants.MGMI_GETGROUPLIST, arguments, APIConstants.CUSTOMER_HOST, APIConstants.CUSTOMER_PORT);
		machines = JSONObject.parseObject(res2);
		JSONArray machineArr =machines.getJSONArray("data");
		for (int i = 0; i < machineArr.size(); i++) {
			JSONObject machine = machineArr.getJSONObject(i);
			machine.put("id", machine.get("gourp_id"));
			machine.put("value", machine.get("gourp_name"));
		}
		cutters = JSONObject.parseObject(res);
		JSONArray cutterArr =cutters.getJSONArray("data");
		for (int i = 0; i < cutterArr.size(); i++) {
			JSONObject cutter = cutterArr.getJSONObject(i);
			cutter.put("id", cutter.get("material_id"));
			cutter.put("value", cutter.get("long_specs_cust"));
		}
		JSONArray operatorArr =operators.getJSONArray("data");
		for (int i = 0; i < operatorArr.size(); i++) {
			JSONObject operator = operatorArr.getJSONObject(i);
			operator.put("id", operator.get("operator_id"));
			operator.put("value", operator.get("real_name"));
		}
		render(cutters,machines,operators);
	}
	
	public static void getNoStartProjectList(){
		JSONObject plans = new JSONObject();
		String state = "0";
		Map<String, Object> planParams = new HashMap<String, Object>();
		planParams.put("customer_id", UserBis.currUser());
		planParams.put("project_states", state);
		planParams.put("project_name",params.get("project_name"));
		Object [] arguments = new Object[]{planParams};
		String res = HproseClinetService.synCall(APIConstants.PJMSI_GETPROJECTNEWLIST, arguments, APIConstants.CUSTOMER_HOST, APIConstants.CUSTOMER_PORT);
		plans = JSONObject.parseObject(res);
		renderJSON(plans); 
	}
	
	public static void addProject(){
		Map<String, Object> planParams = new HashMap<String, Object>();
		planParams.put("customer_id", UserBis.currUser());
		planParams.put("project_data", params.get("project_data"));
		Object [] arguments = new Object[]{planParams};
		LogsUtils.addLogs(Constants.LOG_TYPE_ADD,Convert.convertToInt(UserBis.currOperatorId(),-1), 1, "添加项目执行","success", UserBis.currLoginAccount(), UserBis.currUserName(), UserBis.currUser());
		String res = HproseClinetService.synCall(APIConstants.PJMSI_ADDPROJECT, arguments, APIConstants.CUSTOMER_HOST, APIConstants.CUSTOMER_PORT);
		renderText(res);
	}
	/**
	 * 删除项目（软删除）
	 * 
	 * 2016年1月19日 上午9:40:16
	 * @auth huan208050@vip.qq.com
	 */
	public static void delProject(){
		Map<String, Object> planParams = new HashMap<String, Object>();
		planParams.put("project_id", params.get("project_id"));
		planParams.put("is_delete", "1");
		Object [] arguments = new Object[]{planParams};
		LogsUtils.addLogs(Constants.LOG_TYPE_DEL,Convert.convertToInt(UserBis.currOperatorId(),-1), 1, "删除项目执行","success", UserBis.currLoginAccount(), UserBis.currUserName(), UserBis.currUser());
		String res = HproseClinetService.synCall(APIConstants.PJMSI_DELETEPROJECT, arguments, APIConstants.CUSTOMER_HOST, APIConstants.CUSTOMER_PORT);
		renderText(res);
	}
	
	/**
	 * 修改项目
	* @Title: updateProject 
	* @Description: TODO(这里用一句话描述这个方法的作用) 
	* @param     设定文件 
	* @return void    返回类型 
	* @author chasel 641243343@qq.com   
	* @throws
	 */
	public static void updateProject(){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("customer_id", UserBis.currUser());
		map.put("jsonArr", params.get("jsonArr"));
		Object [] arguments = new Object[]{map};
		LogsUtils.addLogs(Constants.LOG_TYPE_UPDATE,Convert.convertToInt(UserBis.currOperatorId(),-1), 1, "编辑项目执行","success", UserBis.currLoginAccount(), UserBis.currUserName(), UserBis.currUser());
		String res = HproseClinetService.synCall(APIConstants.PJMSI_UPDATEPROJECT, arguments, APIConstants.CUSTOMER_HOST, APIConstants.CUSTOMER_PORT);
		renderText(res);
	}
	
	/**
	 * 根据项目id获取工序
	 * @param project_id
	 * 2016年5月27日 下午2:07:24
	 * @auth liqi
	 */
	public static void getProcessByProjectId(String project_id){
		JSONObject process = new JSONObject();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("project_id",project_id);
		map.put("customer_id", UserBis.currUser());
		Object [] arguments = new Object[]{map};
		String res = HproseClinetService.synCall(APIConstants.PSMSI_GETPROCESSBYPROJECTID, arguments, APIConstants.CUSTOMER_HOST, APIConstants.CUSTOMER_PORT);
		process = JSONObject.parseObject(res);
		renderJSON(process); 
	}
	
	/**
	 * 修改项目
	 * 
	 * 2016年5月31日 下午3:11:19
	 * @auth liqi
	 */
	public static void editProject(){
		Map<String, Object> planParams = new HashMap<String, Object>();
		planParams.put("customer_id", UserBis.currUser());
		planParams.put("project_data", params.get("project_data"));
		Object [] arguments = new Object[]{planParams};
		LogsUtils.addLogs(Constants.LOG_TYPE_UPDATE,Convert.convertToInt(UserBis.currOperatorId(),-1), 1, "编辑项目执行","success", UserBis.currLoginAccount(), UserBis.currUserName(), UserBis.currUser());
		String res = HproseClinetService.synCall(APIConstants.PJMSI_EDITPROJECT, arguments, APIConstants.CUSTOMER_HOST, APIConstants.CUSTOMER_PORT);
		renderJSON(res);
	}
	
	/**
	 * 查询班别列表
	 * 
	 * 2016年6月1日 下午5:38:20
	 * @auth liqi
	 */
	public static void getClassesList(){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("customer_id", UserBis.currUser());
		Object [] arguments = new Object[]{map};
		String classess= HproseClinetService.synCall(APIConstants.CMI_GETCLASSESLIST, arguments, APIConstants.CUSTOMER_HOST, APIConstants.CUSTOMER_PORT);
		JSONObject classessJSON = JSONObject.parseObject(classess);
		JSONArray classessArr = classessJSON.getJSONArray("data");
		JSONArray columnDs = new JSONArray();
		JSONArray tableBodys = new JSONArray();
		JSONObject processTitle = new JSONObject();
		processTitle.put("id", "gongxu");
		processTitle.put("header", "工序");
		processTitle.put("fillspace", true);
		columnDs.add(processTitle);
		JSONObject tableBody = new JSONObject();
		tableBody.put("gongxu", params.get("gongxu"));
		
		for (int i = 0; i < classessArr.size(); i++) {
			JSONObject classes = classessArr.getJSONObject(i);
			JSONObject columnD = new JSONObject();
			columnD.put("id", classes.get("class_id"));
			columnD.put("header", classes.get("class_name"));
			columnD.put("fillspace", true);
			columnD.put("editor", "text");
			columnDs.add(columnD);
			tableBody.put(classes.get("class_id").toString(), "");
		}
		tableBodys.add(tableBody);
		JSONObject json = new JSONObject();
		json.put("tableHead", columnDs);
		json.put("tableBodys", tableBodys);
		renderJSON(json);
	}
}
